********************************************************************************
* Purpose: Create mental health contacts for father
* same as for mothers in 3_cr_psyksssy. Only yearly contacts
********************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 


***************************************************************
* appending raw data
**************************************************************

********************************************************************************
* 1. Psychologists/psychiatrists contacts
********************************************************************************
forvalues i = 2010/2019 {
use "$raw\SSSY`i'",clear
keep pnr bruhon honuge kontakt speciale ydtyp SPEC2
keep if inlist(SPEC2, "24", "63")
rename pnr id_father
merge m:1 id_father using "$work\dads_no1", keep (2 3) nogen
rename pnr pnrb
rename id_father pnr
compress
save "$work\psyksssy`i'_f"	, replace
}

forvalues i = 2020/2020 {
use "$raw\SSSY`i'", clear
keep pnr honuge bruhon kontakt speciale ydtyp SPEC2
tostring SPEC2, replace
tostring speciale, replace
tostring pnr, replace format("%12.0f")
tostring honuge, replace
replace pnr = substr("000000000000",1,12 - length(pnr)) + pnr if pnr != "."
replace pnr = "" if pnr == "."
keep if inlist(SPEC2, "24", "63")
rename pnr id_father
merge m:1 id_father using "$work\dads_no1", keep (2 3) nogen
rename pnr pnrb
rename id_father pnr
compress
save "$work\psyksssy`i'_f", replace		
}


forvalues i = 2010/2020 {
forvalues j = 1/5 {
use "$work\dads_no`j'",clear
rename pnr pnrb
rename id_father pnr
merge 1:m pnr using "$work\psyksssy`i'_f", keep(3) nogen
save "$work\psyksssy`i'merged_cprf`j'",replace
}	
}
forvalues i = 2010/2020 {
use "$work\psyksssy`i'merged_cprf1"
forvalues j = 2/5 {
append using "$work\psyksssy`i'merged_cprf`j'"
}
save "$work\psyksssy`i'merged_cprf", replace
}
use "$work\psyksssy2010merged_cprf",clear
forvalues i = 2011/2020 {
append using "$work\psyksssy`i'merged_cprf", force
}
save "$work\psyksssymerged_cprf", replace

**********************************************************************************

use "$work\psyksssymerged_cprf",clear
drop if honuge == ""
gen psyk = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) psyk, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td

drop if gp_date<=mfr_dob // remember to drop pre data...
gen year_at = ceil((gp_date - mfr_dob)/365.25) 
drop if year_at > 2

collapse (sum) psyk, by(pnr mfr_dob year_at)
reshape wide psyk, i(pnr mfr_dob) j(year_at)

foreach var of varlist  psyk1 psyk2{
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpf_`var'
}
rename pnr cprf
compress 
save "$work\psyksssy_f",replace

***************************************************************
*appending raw data
**************************************************************

********************************************************************************
* 2. GP mental health related contact
********************************************************************************
forvalues i = 2010/2019 {
use "$raw\SSSY`i'",clear
keep pnr bruhon honuge kontakt speciale ydtyp SPEC2
keep if inlist(speciale,"802147","802148", "804106", "806101","802149")
rename pnr id_father
merge m:1 id_father using "$work\dads_no1", keep (2 3) nogen
rename pnr pnrb
rename id_father pnr
compress
save "$work\conssssy`i'_f"	, replace
}
forvalues i = 2020/2020 {
use "$raw\SSSY`i'", clear
keep pnr honuge bruhon kontakt speciale ydtyp SPEC2
tostring SPEC2, replace
tostring speciale, replace
tostring pnr, replace format("%12.0f")
tostring honuge, replace
replace pnr = substr("000000000000",1,12 - length(pnr)) + pnr if pnr != "."
replace pnr = "" if pnr == "."
keep if inlist(speciale,"802147","802148", "804106", "806101","802149")
rename pnr id_father
merge m:1 id_father using "$work\dads_no1", keep (2 3) nogen
rename pnr pnrb
rename id_father pnr
compress
save "$work\conssssy`i'_f", replace		
}


forvalues i = 2010/2020 {
forvalues j = 1/5 {
use "$work\dads_no`j'",clear
rename pnr pnrb
rename id_father pnr
merge 1:m pnr using "$work\conssssy`i'_f", keep(3) nogen
save "$work\conssssy`i'merged_cprf`j'",replace
}	
}
forvalues i = 2010/2020 {
use "$work\conssssy`i'merged_cprf1"
forvalues j = 2/5 {
append using "$work\conssssy`i'merged_cprf`j'"
}
save "$work\conssssy`i'merged_cprf", replace
}
use "$work\conssssy2010merged_cprf",clear
forvalues i = 2011/2020 {
append using "$work\conssssy`i'merged_cprf", force
}
save "$work\conssssymerged_cprf", replace



********************************************************************************
* code outcomes

********************************************************************************

use "$work\conssssymerged_cprf",clear
drop if honuge == ""
gen cons = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace

replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) cons, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td

drop if gp_date<=mfr_dob

gen year_at = ceil((gp_date - mfr_dob)/365.25) 
drop if year_at > 2

collapse (sum) cons, by(pnr mfr_dob year_at)
reshape wide cons, i(pnr mfr_dob) j(year_at)

foreach var of varlist cons1 cons2 {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpf_`var'
}
rename pnr cprf
compress 
save "$work\conssssy_f",replace


********************************************************************************
* 3. Hospital contacts
********************************************************************************
use "$raw\PSYKADM2018",clear
keep if inrange(substr(C_ADIAG,1,4), "DF00","DF99")
save "$work\PSYKADM",replace

forvalues i = 1/5 {
use "$work\dads_no`i'", clear
rename pnr pnrb
rename id_father pnr
merge 1:m pnr using "$work\PSYKADM", keep(3) nogen
rename pnr cprf
save "$work\altpsykkontaktmerged_`i'_f",replace
}
use "$work\altpsykkontaktmerged_1_f",clear
forvalues i =2/5 {
append using "$work\altpsykkontaktmerged_`i'_f"	
}
save "$work\altpsykkontakt_f",replace

forvalues i = 1/5 {
erase "$work\altpsykkontaktmerged_`i'_f.dta"	
}

use "$work\altpsykkontakt_f",clear
format mfr_dob %td
gen date = D_INDDTO
format date %td

drop if date > mfr_dob + 730.5

drop if date<=mfr_dob //  drop pre data...

destring C_PATTYPE, replace 

gen postbirthf1y_out = 0
replace postbirthf1y_out = 1 if date >= mfr_dob  & date - mfr_dob <= 365.25 & C_PATTYPE != 0
gen postbirthf2y_out = 0
replace postbirthf2y_out = 1 if date - mfr_dob > 365.25 & date - mfr_dob <= 730.5 & C_PATTYPE != 0

gen postbirthf1y_in = 0
replace postbirthf1y_in = 1 if date >= mfr_dob  & date - mfr_dob <= 365.25 & C_PATTYPE == 0
gen postbirthf2y_in = 0
replace postbirthf2y_in = 1 if date - mfr_dob > 365.25 & date - mfr_dob <= 730.5 & C_PATTYPE == 0

collapse (max)  post*, by(mfr_dob cprf)

save "$work\psykkontaktdads", replace




